package main

import "fmt"
import "time"

func main(){
	arr := []int{0, 1, 2, 3}
	channel1 := make(chan []int)
	channel2 := make(chan []int)

	//开启获取数据协程
	go getData(arr, channel1)
	//开启求平方协程
	go getSquare(channel1, channel2)
	//开启打印结果协程
	go printMsg(channel2)
	//防止main协程执行太快导致无法执行到打印协程
	time.Sleep(time.Second * 3)
}

//获取数据
func getData(arr []int, channel chan []int){
	print("获取数据协程")
	fmt.Println(arr)
	channel <- arr
}

//求平方
func getSquare(channel1 chan []int, channel2 chan []int){
	arr := <- channel1
	var result []int
	for _, v := range arr {
		result = append(result, v * v)
	}
	channel2 <- result
}

//打印信息
func printMsg(channel2 chan []int){
	result := <- channel2
	print("打印协程数据")
	fmt.Println(result);
}
